package com.example.base.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.domain.SysRoleMenuEntity;
import com.example.base.security.domain.vo.SysRoleMenuVo;
import com.example.base.security.service.SysRoleMenuService;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "角色菜单管理")
@RestController
@RequestMapping("sysrolemenu")
public class SysRoleMenuController {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @ApiOperation("添加角色菜单")
    @PostMapping("add")
    public JSONObject addSysRoleMenuController(@Validated @RequestBody SysRoleMenuVo vo) throws GlobalExcetion {
        sysRoleMenuService.addSysRoleMenu(vo);
        return Result.successInfo("添加成功");
    }

    @ApiOperation("获取角色菜单id列表")
    @GetMapping("getrolemenulist")
    public JSONObject getRoleMenuListController(@RequestParam("roleId")Serializable roleId){
        List<SysRoleMenuEntity> list=sysRoleMenuService.getSysRoleMenuEntityRid(roleId);
        if(list==null){
            return Result.successInfo(Collections.EMPTY_LIST);
        }
        List<Integer> ids=list.stream().map(e->e.getMenuId()).collect(Collectors.toList());
        return Result.successInfo(ids);
    }
}
